<template>
	<view class="add">
		<form @submit="onSubmit">
			<view class="item">
				<input v-model="formValue.title" type="text" name="title" placeholder="请输入标题">
			</view>
			<view class="item">
				<input v-model="formValue.author" type="text" name="author" placeholder="请输入作者">
			</view>
			<view class="item">
				<textarea v-model="formValue.content" name="content" placeholder="请输入详细内容"></textarea>
			</view>
			<view class="item">
				<button form-type="reset">重置</button>
				<button form-type="submit" type="primary" :disabled="isDisabled(formValue)">提交</button>
			</view>
		</form>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formValue: {
					title: '',
					author: '',
					content: ''
				}
			};
		},
		methods: {
			isDisabled(obj) {
				for (let key in obj) {
					if (!obj[key]) {
						return true
					}
				}
			},
			onSubmit(e) {
				let detail = e.detail.value
				uniCloud.callFunction({
					name: 'art_add_row',
					data: {
						detail
					}
				}).then(res => {
					uni.showToast({
						title: '发布成功'
					})
					setTimeout(() => {
						uni.reLaunch({
							url: '/pages/index/index'
						})
					}, 800)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		padding: 30upx;
		.item {
			padding-bottom: 20upx;
			input, textarea {
				border: 1upx solid #eee;
				height: 80upx;
				padding: 0 20upx;
			}
			textarea {
				height: 200upx;
				width: 100%;
				box-sizing: border-box;
			}
			button {
				margin-bottom: 20upx;
			}
		}
	}
</style>
